<?php

namespace App\Jobs;

use App\Http\Helpers\Sign;
use App\Http\Services\FaceService;
use App\Http\Services\MsgService;
use App\Models\Faces;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class FacePushJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    const APPKEY = "11F7AB57AB3E32D4";
    const APPSECRET = "ACB5C12C15B4525C3643E728DC8EF120";

    public $data;
    public $validate;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data, $validate)
    {
        $this->data = $data;
        $this->validate = $validate;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if($this->validate){
            if(empty($this->data) || !isset($this->data['appKey'])){
                return MsgService::getMsg(1009);
            }
            if($this->data['appKey'] != self::APPKEY){
                return MsgService::getMsg(1008);
            }
            $sign = Sign::colsign($this->data, self::APPSECRET);
            if($this->data['signature'] != $sign){
                Sign::writeLog('签名验证：', '验证失败！', 'face');
                return MsgService::getMsg(1005);
            }
            Sign::writeLog('签名验证：', '验证成功！', 'face');
        }else{
            Sign::writeLog('签名验证：', '不校验签名！', 'face');
        }

        $push = [
            "card_number" => $this->data['cardNumber'],
            "school_code" => $this->data['schoolCode'],
            "image_url" => $this->data['imageUrl'],
            "nonce" => $this->data['nonce'],
            "signature" => $this->data['signature']
        ];
        $base64ImageContent = FaceService::imgBase64Encode($this->data['imageUrl']) ?: '';
        $facePath = FaceService::imgBase64Decode($base64ImageContent, true, 'public/uploads/images/face/');
        $facePath = str_replace('public/', '/', $facePath);
        Sign::writeLog('保存本地：', $facePath, 'face');
        $push['face'] = $facePath;
        $res = self::add($push);
        if($res){
            Sign::writeLog('人脸推送状态：', '推送成功_id：'.$res->id, 'face');
            return json_encode(['result' => 1,' msg' => 'ok']);
        }else{
            Sign::writeLog('人脸推送状态：', '推送失败：存库失败', 'face');
            return MsgService::getMsg(-1);
        }
    }

    /**
     *人脸参数保存
     * @author lican<364548233@qq.com>
     */
    public static function add($data){
        $face = Faces::where('school_code', $data['school_code'])
            ->where('card_number', $data['card_number'])
            ->first();
        if(!$face){
            $face = new Faces();
        }
        $face->school_code = $data['school_code'];
        $face->card_number = $data['card_number'];
        $face->nonce = $data['nonce'];
        $face->image_url = $data['image_url'];
        $face->face = $data['face'];
        $face->save();
        return $face;
    }

}
